package com.baizhi.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.service.CmfzGuruService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljh
 * @since 2020-08-18
 */
@RestController
@RequestMapping("/cmfzGuru")
public class CmfzGuruController {

    @Autowired
    private CmfzGuruService cmfzGuruService;

    @Value("${imgPath}")
    private String imgPath;

    /**
     * 模糊+分页查询
     * @param page
     * @param limit
     * @param key
     * @return
     */
    @RequestMapping("/selectGuruByPage")
    public Map<String,Object> selectGuruByPage(Integer page,Integer limit,
                                               @RequestParam(required = false) String key){
        HashMap<String, Object> map = new HashMap<>();

        try{
            PageInfo<CmfzGuru> pageInfo=cmfzGuruService.selectGuruByPageAndCondition(page,limit,key);
            System.out.println(pageInfo.getList());
            map.put("code",0);
            map.put("count",pageInfo.getTotal());
            map.put("data",pageInfo.getList());
            map.put("msg","查询成功");
        }catch (Exception e){
            map.put("code",1);
            map.put("msg","查询失败");
        }
        return map;
    }

    /**
     * 头像的上传
     * @param file
     * @return
     */
    @RequestMapping("/uploadGuruImg")
    public Map<String,Object> uploadGuruImg(MultipartFile file){
        HashMap<String, Object> map = new HashMap<>();
        try{
            file.transferTo(new File(imgPath,file.getOriginalFilename()));
            map.put("code",0);
            map.put("path",file.getOriginalFilename());
            map.put("msg","上传成功");
        }catch (Exception e){
            map.put("code",1);
            map.put("msg","上传失败");
        }
        return map;
    }

    @RequestMapping("/addGuru")
    public Map<String,Object> addGuru(CmfzGuru cmfzGuru){
        HashMap<String, Object> map = new HashMap<>();
        try{
            cmfzGuruService.addGuru(cmfzGuru);
            map.put("code",0);
            map.put("msg","新增成功");
        }catch (Exception e){
            map.put("code",1);
            map.put("msg","新增失败");
        }
        return map;
    }


    /**
     * 修改状态
     * @param guruId
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    public Map<String,Object> updateStatus(Integer guruId,Integer status){
        System.out.println(guruId);
        System.out.println(status);
        HashMap<String, Object> map = new HashMap<>();
        try{
            cmfzGuruService.updateGuruStatus( guruId,status);
            map.put("code",0);
            map.put("msg","修改成功");
        }catch (Exception e){
            map.put("code",1);
            map.put("msg","修改失败");
        }
        return map;
    }


    @RequestMapping("/downExcel")
    public void downExcel(HttpServletResponse response) throws IOException {
        List<CmfzGuru> cmfzGurus = cmfzGuruService.list();
        ExportParams exportParams = new ExportParams("标题","gurus");
        /**
         * exportExcel 导出Excel文件的方法
         * 参数1 导出参数对象
         * 参数2 pojoClass 普通Java对象的类对象 要到处的实体类的类对象
         * 参数3 要导出的数据 集合
         */
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CmfzGuru.class, cmfzGurus);
        response.setContentType( "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("gurus.xls","utf-8"));
        OutputStream out = response.getOutputStream();
        workbook.write(out);
        out.close();
    }

    @RequestMapping("/uploadExcel")
    public Map<String,Object> uploadExcel(MultipartFile file) throws Exception {

        System.out.println(file);
        HashMap<String, Object> map = new HashMap<>();
        InputStream inputStream = file.getInputStream();
        /**
         * setTitleRows(1) 设置标题所占的行数
         * setHeadRows(1) 设置表头所占的行数
         */
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setHeadRows(1);
        /**
         * 参数1 流
         * 参数2 pojoClass 普通Java对象的类对象 要导入的实体类的类对象
         * 参数3 导入参数对象
         */
        List<CmfzGuru> list = ExcelImportUtil.importExcel(inputStream, CmfzGuru.class, importParams);

        for (CmfzGuru cmfzGuru : list) {
            cmfzGuruService.save(cmfzGuru);
        }
        map.put("msg","上传成功");
        return map;
    }


}
